/*
 * BSM6.0
 *
 * Copyright 2012-2013 (C) SINODATA CO., LTD. All Rights Reserved.
 */
package com.sinodata.bsm.cicp.util.pool;

import java.util.HashMap;
import java.util.Map;

import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import javax.naming.Context;

import org.apache.commons.pool.PoolableObjectFactory;

/**
 * <p>
 * Description: JMX的连接池对象    
 * [TIP]: JMX的连接目前未使用连接池 
 * </p>
 *
 * @author wangyazhou
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-10-26 上午9:16:01          wangyazhou        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
public class JMXPoolableObject implements PoolableObjectFactory {

    public static final String WEBLOGIC = "weblogic9+";

    public static final String WEBSPHERE = "was";

    public static final String WEBSPHERE7 = "was7";

    public static final String TOMCAT = "tomcat";

    private String type = "";

    private String ip = "";

    private int port = 0;

    private String user = "";

    private String pwd = "";

    public JMXPoolableObject(String type, String ip, int port, String user, String pwd) {
        this.type = type;
        this.port = port;
        this.ip = ip;
        this.user = user;
        this.pwd = pwd;
    }

    @Override
    public void activateObject(Object arg0) throws Exception {

    }

    @Override
    public void destroyObject(Object obj) throws Exception {
        BSMJMXConnector conn = (BSMJMXConnector) obj;
        conn.close();
    }

    @Override
    public Object makeObject() throws Exception {
        String protocol = "iiop";
        String jndiPath = "/jndi/";
        Map<String, Object> h = new HashMap<String, Object>();
        if (WEBLOGIC.equals(type)) {
            protocol = "t3";
            jndiPath = "/jndi/weblogic.management.mbeanservers.domainruntime";
            h.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES, "weblogic.management.remote");
        } else if (WEBSPHERE.equals(type)) {
            protocol = "iiop";
            jndiPath = "/jndi/JMXConnector";
        } else if (TOMCAT.equals(type)) {
            protocol = "rmi:///jndi/rmi";
            jndiPath = "/jmxrmi";
        }
        String serviceURLString = "service:jmx" + ":" + protocol + "://" + ip + ":" + port + jndiPath;
        JMXServiceURL serviceURL;
        BSMJMXConnector connector = null;

        serviceURL = new JMXServiceURL(serviceURLString);
        h.put(Context.SECURITY_PRINCIPAL, user);
        h.put(Context.SECURITY_CREDENTIALS, pwd);
        connector = (BSMJMXConnector) JMXConnectorFactory.connect(serviceURL, h);
        connector.setValid(true);

        return connector;

    }

    @Override
    public void passivateObject(Object arg0) throws Exception {

    }

    @Override
    public boolean validateObject(Object obj) {
        BSMJMXConnector conn = (BSMJMXConnector) obj;
        return conn.isValid();
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }
}
